﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meebey.SmartIrc4net;
using BotTorrent;
using System.Reflection;
using XmlSettings;

namespace TorrentService
{
    public class ircbot : IrcClient
{
    // Fields
    public string AuthedUser;
    public BitTorrentClient BTClient;
    public IrcMethods IrcCommands = new IrcMethods();
    public RssFeedManager RssFeedManager;
    public Settings Settings;

    // Methods
    private void eventOnChannelMessage(object sender, IrcEventArgs e)
    {
        string[] command = e.Data.MessageArray;
        if (command[0].ToUpper() == "!LOGIN")
        {
            miscFunctions._ircLogin(e, this, command);
        }
        else if ((e.Data.Nick + "@" + e.Data.Host) == this.AuthedUser)
        {
            if (command[0].ToUpper() == "!HELP")
            {
                MethodInfo[] milist = this.IrcCommands.GetType().GetMethods();
                foreach (MethodInfo mi in milist)
                {
                    if ((((mi.Name != "Equals") && (mi.Name != "ToString")) && (mi.Name != "GetType")) && (mi.Name != "GetHashCode"))
                    {
                        string message = "!" + mi.Name.ToString();
                        base.SendMessage(SendType.Message, e.Data.Channel, message);
                    }
                }
            }
            else if (command[0].StartsWith("!"))
            {
                try
                {
                    string methodtocall = command[0].TrimStart(new char[] { '!' });
                    string messagesource = miscFunctions.GetSource(e);
                    MethodInfo mi = this.IrcCommands.GetType().GetMethod(methodtocall.ToUpper());
                    object[] arrParms = new object[3];
                    arrParms.SetValue(e, 0);
                    arrParms.SetValue(this, 1);
                    arrParms.SetValue(command, 2);
                    List<string> messagelist = (List<string>) mi.Invoke(this.IrcCommands, arrParms);
                    foreach (string message in messagelist)
                    {
                        base.SendMessage(SendType.Message, messagesource, message);
                    }
                }
                catch (Exception error)
                {
                    base.SendMessage(SendType.Message, e.Data.Channel, error.Message);
                }
            }
        }
    }

    private void eventOnConnected(object sender, EventArgs e)
    {
        try
        {
            base.Login(this.Settings.IrcSettings. ircNickPrefix, "Torrent IRC", 0, miscFunctions.RandomString(8, true));
            base.RfcJoin(this.Settings.IrcSettings.ircChannel, this.Settings.IrcSettings.ircKey);
            base.Listen();
        }
        catch (NotConnectedException ex)
        {
            //todo this has been thrown before, dont know what to do after
        }
    }

    private void eventOnConnecting(object sender, EventArgs e)
    {
    }

    private void eventOnDisconnected(object sender, EventArgs e)
    {
    }

    public void Initialise()
    {
        base.Encoding = Encoding.UTF8;
        base.SendDelay = 200;
        base.AutoReconnect = true;
        base.AutoRejoin = true;
        base.AutoRejoinOnKick = true;
        base.AutoRelogin = true;
        base.AutoRetry = true;
        base.AutoNickHandling = false;
        base.CtcpVersion = "BotTorrent 1.0";
        base.SendDelay = 500;
        base.AutoRetry = true;
        base.AutoRetryDelay = 30;
        base.ActiveChannelSyncing = true;
        base.OnChannelMessage += new IrcEventHandler(this.eventOnChannelMessage);
        base.OnQueryMessage += new IrcEventHandler(this.eventOnChannelMessage);
        base.OnConnected += new EventHandler(this.eventOnConnected);
        base.OnConnecting += new EventHandler(this.eventOnConnecting);
        base.OnDisconnected += new EventHandler(this.eventOnDisconnected);
        while (true)
        {
            try
            {
                base.Connect(this.Settings.IrcSettings.ircServer, this.Settings.IrcSettings.ircServerPort);

            }
            catch (Exception e) { }; //already have catch in place elsewhere    
        }
    }
}

 


}
